﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;

namespace WOLHelper.Client
{
    public partial class WOLService : ServiceBase
    {
        public WOLService()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
        }

        protected override void OnStop()
        {
        }

        private void Connect()
        {
            this.RegisterClient();           
        }
        
        #region "callback services"

        public class BroadcastorCallback : WOLHelperServiceReference.IBroadcastorServiceCallback
        {
            private System.Threading.SynchronizationContext syncContext = AsyncOperationManager.SynchronizationContext;

            private EventHandler _broadcastorCallBackHandler;
            public void SetHandler(EventHandler handler)
            {
                this._broadcastorCallBackHandler = handler;
            }

            public void BroadcastToClient(WOLHelperServiceReference.EventDataType eventData)
            {
                syncContext.Post(new System.Threading.SendOrPostCallback(OnBroadcast), eventData);
            }

            private void OnBroadcast(object eventData)
            {
                this._broadcastorCallBackHandler.Invoke(eventData, null);
            }
        }

        private delegate void HandleBroadcastCallback(object sender, EventArgs e);
        public void HandleBroadcast(object sender, EventArgs e)
        {
            try
            {
                var eventData = (WOLHelperServiceReference.EventDataType)sender;

                Console.WriteLine(string.Format("{0} (from {1})", eventData.EventMessage, eventData.ClientName));
            }
            catch (Exception ex)
            {
            }
        }

        #endregion

        private WOLHelperServiceReference.BroadcastorServiceClient _client;

        private void RegisterClient()
        {
            if ((this._client != null))
            {
                this._client.Abort();
                this._client = null;
            }

            BroadcastorCallback cb = new BroadcastorCallback();
            cb.SetHandler(this.HandleBroadcast);

            System.ServiceModel.InstanceContext context = 
                new System.ServiceModel.InstanceContext(cb);
            this._client = 
                new WOLHelperServiceReference.BroadcastorServiceClient(context);

            this._client.RegisterClient("Test");
        }

        private void btnRegisterClient_Click(object sender, EventArgs e)
        {
            this.RegisterClient();
        }

        private void btnSendEvent_Click(object sender, EventArgs e)
        {
            if (this._client == null)
            {
                Console.WriteLine("Client is not registered");
                return;
            }
            else
            {
                this._client.NotifyServer(
                    new WOLHelperServiceReference.EventDataType()
                    {
                        ClientName = "Test",
                        EventMessage = "MessageA"
                    });
            }
        }
    }
}
